namespace SilverSea.LPRProcessing
{
    using lpr;
    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Drawing;
    using System.Drawing.Imaging;
    using System.Threading;

    public class SimpleLPRProcessing : ILPRProcessing
    {
        private bool bool_0;
        private int int_0 = 8;
        private int int_1 = 120;
        private IProcessor iprocessor_0;
        private ISimpleLPR isimpleLPR_0;
        private PlateFeatures plateFeatures_0 = PlateFeatures.Both;
        private PlateNumberFixing plateNumberFixing_0;
        private string string_0 = string.Empty;
        private string string_1 = "VN";
        private string string_2 = string.Empty;

        public event ErrorEventHandler ErrorEvent;

        public bool Analyze(string fileName, ref string[] plateNumber)
        {
            if (this.iprocessor_0 != null)
            {
                // bool flag;
                try
                {
                    try
                    {
                        List<Candidate> list = this.iprocessor_0.analyze(fileName, (uint) this.int_1);
                        if ((list != null) && (list.Count > 0))
                        {
                            plateNumber = new string[list.Count];
                            for (int i = 1; i < list.Count; i++)
                            {
                                plateNumber[i] = list[i].text;
                                plateNumber[i] = this.plateNumberFixing_0.Fix(plateNumber[i]);
                            }
                            return true;
                        }
                    }
                    catch (Exception exception)
                    {
                        this.CloseLPREngine();
                        Thread.Sleep(0x3e8);
                        this.CreateLPREngine();
                        if (this.ErrorEvent != null)
                        {
                            this.ErrorEvent(this, new ErrorEventArgs(exception.ToString()));
                        }
                    }
                    goto Label_00B3;
                }
                finally
                {
                    GC.Collect();
                }
                //// return flag;
            }
        Label_00B3:
            return false;
        }

        public bool Analyze(string fileName, ref string plateNumber)
        {
            if (this.iprocessor_0 != null)
            {
                // bool flag;
                try
                {
                    try
                    {
                        List<Candidate> list = this.iprocessor_0.analyze(fileName, (uint) this.int_1);
                        if ((list != null) && (list.Count > 0))
                        {
                            Candidate candidate = list[0];
                            for (int i = 1; i < list.Count; i++)
                            {
                                if (list[i].confidence > candidate.confidence)
                                {
                                    candidate = list[i];
                                }
                            }
                            plateNumber = candidate.text;
                            plateNumber = this.plateNumberFixing_0.Fix(plateNumber);
                            return true;
                        }
                    }
                    catch (Exception exception)
                    {
                        this.CloseLPREngine();
                        Thread.Sleep(0x3e8);
                        this.CreateLPREngine();
                        if (this.ErrorEvent != null)
                        {
                            this.ErrorEvent(this, new ErrorEventArgs(exception.ToString()));
                        }
                    }
                    goto Label_00C2;
                }
                finally
                {
                    GC.Collect();
                }
                // return flag;
            }
        Label_00C2:
            return false;
        }

        public bool Analyze(string fileName, ref string[] plateNumber, ref Bitmap[] plateImage)
        {
            if (this.iprocessor_0 != null)
            {
                // bool flag;
                try
                {
                    try
                    {
                        Bitmap bitmap = (Bitmap) Image.FromFile(fileName);
                        List<Candidate> list = this.iprocessor_0.analyze(fileName, (uint) this.int_1);
                        if ((list != null) && (list.Count > 0))
                        {
                            plateNumber = new string[list.Count];
                            plateImage = new Bitmap[list.Count];
                            for (int i = 0; i < list.Count; i++)
                            {
                                plateNumber[i] = list[i].text;
                                plateNumber[i] = this.plateNumberFixing_0.Fix(plateNumber[i]);
                                if (plateNumber[i] != string.Empty)
                                {
                                    Rectangle empty = Rectangle.Empty;
                                    plateImage[i] = this.method_1(bitmap, list[i], ref empty);
                                }
                            }
                            return true;
                        }
                    }
                    catch (Exception exception)
                    {
                        this.CloseLPREngine();
                        Thread.Sleep(0x3e8);
                        this.CreateLPREngine();
                        if (this.ErrorEvent != null)
                        {
                            this.ErrorEvent(this, new ErrorEventArgs(exception.ToString()));
                        }
                    }
                    goto Label_00FD;
                }
                finally
                {
                    GC.Collect();
                }
                // return flag;
            }
        Label_00FD:
            return false;
        }

        public bool Analyze(string fileName, ref string plateNumber, ref Bitmap plateImage)
        {
            if (this.iprocessor_0 != null)
            {
                // bool flag;
                try
                {
                    try
                    {
                        Bitmap bitmap = new Bitmap(Image.FromFile(fileName));
                        List<Candidate> list = this.iprocessor_0.analyze(fileName, (uint) this.int_1);
                        if ((list != null) && (list.Count > 0))
                        {
                            Candidate candidate = list[0];
                            for (int i = 1; i < list.Count; i++)
                            {
                                if (list[i].confidence > candidate.confidence)
                                {
                                    candidate = list[i];
                                }
                            }
                            plateNumber = candidate.text;
                            plateNumber = this.plateNumberFixing_0.Fix(plateNumber);
                            if (plateNumber != string.Empty)
                            {
                                Rectangle empty = Rectangle.Empty;
                                plateImage = this.method_1(bitmap, candidate, ref empty);
                            }
                            return true;
                        }
                    }
                    catch (Exception exception)
                    {
                        this.CloseLPREngine();
                        Thread.Sleep(0x3e8);
                        this.CreateLPREngine();
                        if (this.ErrorEvent != null)
                        {
                            this.ErrorEvent(this, new ErrorEventArgs(exception.ToString()));
                        }
                    }
                    goto Label_00F1;
                }
                finally
                {
                    GC.Collect();
                }
                // return flag;
            }
        Label_00F1:
            return false;
        }

        public bool Analyze(string fileName, Bitmap vehicleImage, ref string plateNumber, ref int recognizeTime, ref Bitmap plateImage)
        {
            int num = recognizeTime;
            if (this.iprocessor_0 != null)
            {
                // bool flag;
                try
                {
                    try
                    {
                        Stopwatch stopwatch = new Stopwatch();
                        stopwatch.Start();
                        List<Candidate> list = this.iprocessor_0.analyze(fileName, (uint) this.int_1);
                        stopwatch.Stop();
                        recognizeTime = stopwatch.Elapsed.Milliseconds;
                        if ((list != null) && (list.Count > 0))
                        {
                            Candidate candidate = list[0];
                            for (int i = 1; i < list.Count; i++)
                            {
                                if (list[i].confidence > candidate.confidence)
                                {
                                    candidate = list[i];
                                }
                            }
                            plateNumber = candidate.text;
                            plateNumber = this.plateNumberFixing_0.Fix(plateNumber);
                            if (plateNumber != string.Empty)
                            {
                                int count = candidate.elements.Count;
                                int height = vehicleImage.Height;
                                int num5 = 0;
                                foreach (Element element in candidate.elements)
                                {
                                    if (element.bbox.Y < height)
                                    {
                                        height = element.bbox.Y;
                                    }
                                    if ((element.bbox.Y + element.bbox.Height) > num5)
                                    {
                                        num5 = element.bbox.Y + element.bbox.Height;
                                    }
                                }
                                int x = candidate.elements[0].bbox.X;
                                int y = candidate.elements[0].bbox.Y;
                                int width = candidate.elements[count - 1].bbox.Width;
                                int num9 = candidate.elements[count - 1].bbox.Height;
                                int num10 = candidate.elements[count - 1].bbox.X + width;
                                int num1 = candidate.elements[count - 1].bbox.Y;
                                int num11 = x - ((3 * width) / 2);
                                int num12 = height - (num9 / 2);
                                int num13 = (num10 - x) + (3 * width);
                                int num14 = (num5 - height) + num9;
                                if (num11 < 0)
                                {
                                    num11 = 0;
                                }
                                if (num12 < 0)
                                {
                                    num12 = 0;
                                }
                                if ((num11 + num13) > vehicleImage.Width)
                                {
                                    num13 = vehicleImage.Width - num11;
                                }
                                if ((num12 + num14) > vehicleImage.Height)
                                {
                                    num14 = vehicleImage.Height - num12;
                                }
                                if (((num11 >= 0) && (num12 >= 0)) && ((num13 >= 0) && (num14 >= 0)))
                                {
                                    Rectangle cropArea = new Rectangle(num11, num12, num13, num14);
                                    plateImage = Untils.CropImage(vehicleImage, cropArea);
                                }
                                if (num > 0)
                                {
                                    Graphics graphics = Graphics.FromImage(vehicleImage);
                                    if ((y - (6 * num9)) > 0)
                                    {
                                        graphics.DrawString("+", new Font("Digital-7", 80f), Brushes.Red, new PointF((float) (x - (2 * width)), (float) (y - (6 * num9))));
                                        if (((x - (2 * width)) < 250) && ((y - (6 * num9)) < 250))
                                        {
                                            graphics.DrawString(num.ToString() + " Km/h", new Font("Digital-7", 40f), Brushes.Red, new PointF(0f, (float) (vehicleImage.Height - 60)));
                                        }
                                        else
                                        {
                                            graphics.DrawString(num.ToString() + "\nKm/h", new Font("Digital-7", 40f), Brushes.Red, new PointF(0f, 0f));
                                        }
                                    }
                                    else
                                    {
                                        graphics.DrawString("+", new Font("Digital-7", 80f), Brushes.Red, new PointF((float) (x - (2 * width)), (float) (y + (num9 / 3))));
                                        graphics.DrawString(num.ToString() + " Km/h", new Font("Digital-7", 40f), Brushes.Red, new PointF(0f, (float) (vehicleImage.Height - 60)));
                                    }
                                    vehicleImage.Save(fileName, ImageFormat.Jpeg);
                                    graphics.Dispose();
                                }
                            }
                            return true;
                        }
                    }
                    catch (Exception exception)
                    {
                        this.CloseLPREngine();
                        Thread.Sleep(0x3e8);
                        this.CreateLPREngine();
                        if (this.ErrorEvent != null)
                        {
                            this.ErrorEvent(this, new ErrorEventArgs(exception.ToString()));
                        }
                    }
                    goto Label_0491;
                }
                finally
                {
                    GC.Collect();
                }
                // return flag;
            }
        Label_0491:
            return false;
        }

        public bool Analyze(string fileName, Bitmap vehicleImage, ref string[] plateNumber, ref int recognizeTime, ref Bitmap[] plateImage, ref float[] confidence, ref bool[] isBlackOnWhite)
        {
            if (this.iprocessor_0 != null)
            {
                // bool flag;
                try
                {
                    try
                    {
                        Stopwatch stopwatch = new Stopwatch();
                        stopwatch.Start();
                        List<Candidate> list = this.iprocessor_0.analyze(fileName, (uint) this.int_1);
                        stopwatch.Stop();
                        recognizeTime = stopwatch.Elapsed.Milliseconds;
                        if ((list != null) && (list.Count > 0))
                        {
                            plateNumber = new string[list.Count];
                            plateImage = new Bitmap[list.Count];
                            confidence = new float[list.Count];
                            isBlackOnWhite = new bool[list.Count];
                            for (int i = 0; i < list.Count; i++)
                            {
                                plateNumber[i] = list[i].text;
                                plateNumber[i] = this.plateNumberFixing_0.Fix(plateNumber[i]);
                                confidence[i] = list[i].confidence;
                                isBlackOnWhite[i] = list[i].brightBackground;
                                if (plateNumber[i] != string.Empty)
                                {
                                    Rectangle empty = Rectangle.Empty;
                                    plateImage[i] = this.method_1(vehicleImage, list[i], ref empty);
                                }
                            }
                            return true;
                        }
                    }
                    catch (Exception exception)
                    {
                        this.CloseLPREngine();
                        Thread.Sleep(0x3e8);
                        this.CreateLPREngine();
                        if (this.ErrorEvent != null)
                        {
                            this.ErrorEvent(this, new ErrorEventArgs(exception.ToString()));
                        }
                    }
                    goto Label_0158;
                }
                finally
                {
                    GC.Collect();
                }
                // return flag;
            }
        Label_0158:
            return false;
        }

        public bool Analyze(string fileName, Bitmap vehicleImage, ref string plateNumber, ref int recognizeTime, ref Bitmap plateImage, ref float confidence, ref bool isBlackOnWhite)
        {
            if (this.iprocessor_0 != null)
            {
                // bool flag;
                try
                {
                    try
                    {
                        Stopwatch stopwatch = new Stopwatch();
                        stopwatch.Start();
                        List<Candidate> list = this.iprocessor_0.analyze(fileName, (uint) this.int_1);
                        stopwatch.Stop();
                        recognizeTime = stopwatch.Elapsed.Milliseconds;
                        if ((list != null) && (list.Count > 0))
                        {
                            Candidate candidate = list[0];
                            for (int i = 1; i < list.Count; i++)
                            {
                                if (list[i].confidence > candidate.confidence)
                                {
                                    candidate = list[i];
                                }
                            }
                            plateNumber = candidate.text;
                            plateNumber = this.plateNumberFixing_0.Fix(plateNumber);
                            confidence = candidate.confidence;
                            isBlackOnWhite = candidate.brightBackground;
                            if (plateNumber != string.Empty)
                            {
                                Rectangle empty = Rectangle.Empty;
                                plateImage = this.method_1(vehicleImage, candidate, ref empty);
                            }
                            return true;
                        }
                    }
                    catch (Exception exception)
                    {
                        this.CloseLPREngine();
                        Thread.Sleep(0x3e8);
                        this.CreateLPREngine();
                        if (this.ErrorEvent != null)
                        {
                            this.ErrorEvent(this, new ErrorEventArgs(exception.ToString()));
                        }
                    }
                    goto Label_0124;
                }
                finally
                {
                    GC.Collect();
                }
                // return flag;
            }
        Label_0124:
            return false;
        }

        public bool Analyze(string fileName, Bitmap vehicleImage, ref string plateNumber, ref int recognizeTime, ref Bitmap plateImage, string overlayText, int overlayFontSize, Color overlayColor)
        {
            if (this.iprocessor_0 != null)
            {
                // bool flag;
                try
                {
                    try
                    {
                        Stopwatch stopwatch = new Stopwatch();
                        stopwatch.Start();
                        List<Candidate> list = this.iprocessor_0.analyze(fileName, (uint) this.int_1);
                        stopwatch.Stop();
                        recognizeTime = stopwatch.Elapsed.Milliseconds;
                        if ((list != null) && (list.Count > 0))
                        {
                            Candidate candidate = list[0];
                            for (int i = 1; i < list.Count; i++)
                            {
                                if (list[i].confidence > candidate.confidence)
                                {
                                    candidate = list[i];
                                }
                            }
                            plateNumber = candidate.text;
                            if (plateNumber != string.Empty)
                            {
                                plateNumber = this.plateNumberFixing_0.Fix(plateNumber);
                                Rectangle empty = Rectangle.Empty;
                                plateImage = this.method_1(vehicleImage, candidate, ref empty);
                                if (overlayText != string.Empty)
                                {
                                    Graphics graphics = Graphics.FromImage(vehicleImage);
                                    Brush brush = new SolidBrush(overlayColor);
                                    Font font = new Font("Microsoft Sans Serif", (float) overlayFontSize);
                                    if ((empty.Y < (vehicleImage.Width / 2)) && (empty.Width != 0))
                                    {
                                        graphics.DrawString(DateTime.Now.ToString(), font, brush, new PointF(0f, (float) ((vehicleImage.Height - overlayFontSize) - 10)));
                                    }
                                    else
                                    {
                                        graphics.DrawString(DateTime.Now.ToString(), font, brush, new PointF(0f, 0f));
                                    }
                                    vehicleImage.Save(fileName, ImageFormat.Jpeg);
                                    brush.Dispose();
                                    graphics.Dispose();
                                }
                            }
                            return true;
                        }
                    }
                    catch (Exception exception)
                    {
                        this.CloseLPREngine();
                        Thread.Sleep(0x3e8);
                        this.CreateLPREngine();
                        if (this.ErrorEvent != null)
                        {
                            this.ErrorEvent(this, new ErrorEventArgs(exception.ToString()));
                        }
                    }
                    goto Label_01DE;
                }
                finally
                {
                    GC.Collect();
                }
                // return flag;
            }
        Label_01DE:
            return false;
        }

        public void CloseLPREngine()
        {
            if (this.iprocessor_0 != null)
            {
                this.iprocessor_0.Dispose();
            }
            if (this.isimpleLPR_0 != null)
            {
                this.isimpleLPR_0.Dispose();
            }
        }

        public void CreateLPREngine()
        {
            this.isimpleLPR_0 = SimpleLPR.Setup();
            this.method_0();
            this.plateNumberFixing_0 = new PlateNumberFixing();
            this.plateNumberFixing_0.CountryCode = this.string_1;
            this.plateNumberFixing_0.ProvinceCode = this.string_2;
            this.plateNumberFixing_0.PlateFeature = this.plateFeatures_0;
            this.plateNumberFixing_0.EnableMotorUnder50cc = this.bool_0;
        }

        private void method_0()
        {
            try
            {
                if (this.iprocessor_0 == null)
                {
                    this.iprocessor_0 = this.isimpleLPR_0.createProcessor();
                }
                for (uint i = 0; i < this.isimpleLPR_0.numSupportedCountries; i++)
                {
                    string str = this.isimpleLPR_0.get_countryCode(i);
                    this.isimpleLPR_0.set_countryWeight(str, str.ToLower().Contains(this.string_1) ? 1f : 0f);
                }
                this.isimpleLPR_0.realizeCountryWeights();
            }
            catch (Exception exception)
            {
                if (this.ErrorEvent != null)
                {
                    this.ErrorEvent(this, new ErrorEventArgs(exception.ToString()));
                }
            }
        }

        private Bitmap method_1(Bitmap bitmap_0, Candidate candidate_0, ref Rectangle rectangle_0)
        {
            Bitmap bitmap = null;
            int count = candidate_0.elements.Count;
            int height = bitmap_0.Height;
            int num3 = 0;
            foreach (Element element in candidate_0.elements)
            {
                if (element.bbox.Y < height)
                {
                    height = element.bbox.Y;
                }
                if ((element.bbox.Y + element.bbox.Height) > num3)
                {
                    num3 = element.bbox.Y + element.bbox.Height;
                }
            }
            int x = candidate_0.elements[0].bbox.X;
            int y = candidate_0.elements[0].bbox.Y;
            int width = candidate_0.elements[count - 1].bbox.Width;
            int num6 = candidate_0.elements[count - 1].bbox.Height;
            int num7 = candidate_0.elements[count - 1].bbox.X + width;
            int num12 = candidate_0.elements[count - 1].bbox.Y;
            int num8 = x - ((3 * width) / 2);
            int num9 = height - (num6 / 2);
            int num10 = (num7 - x) + (3 * width);
            int num11 = (num3 - height) + num6;
            if (num8 < 0)
            {
                num8 = 0;
            }
            if (num9 < 0)
            {
                num9 = 0;
            }
            if ((num8 + num10) > bitmap_0.Width)
            {
                num10 = bitmap_0.Width - num8;
            }
            if ((num9 + num11) > bitmap_0.Height)
            {
                num11 = bitmap_0.Height - num9;
            }
            if (((num8 >= 0) && (num9 >= 0)) && ((num10 >= 0) && (num11 >= 0)))
            {
                rectangle_0 = new Rectangle(num8, num9, num10, num11);
                bitmap = Untils.CropImage(bitmap_0, rectangle_0);
            }
            return bitmap;
        }

        public string CountryCode
        {
            get
            {
                return this.string_1;
            }
            set
            {
                this.string_1 = value;
            }
        }

        public bool EnableMotorUnder50cc
        {
            get
            {
                return this.bool_0;
            }
            set
            {
                this.bool_0 = value;
            }
        }

        public string LPREngineProductKey
        {
            get
            {
                return this.string_0;
            }
            set
            {
                this.string_0 = value;
            }
        }

        public int MaxCharHeight
        {
            get
            {
                return this.int_1;
            }
            set
            {
                this.int_1 = value;
            }
        }

        public int MinCharHeight
        {
            get
            {
                return this.int_0;
            }
            set
            {
                this.int_0 = value;
            }
        }

        public PlateFeatures PlateFeature
        {
            get
            {
                return this.plateFeatures_0;
            }
            set
            {
                this.plateFeatures_0 = value;
            }
        }

        public string ProvinceCode
        {
            get
            {
                return this.string_2;
            }
            set
            {
                this.string_2 = value;
            }
        }
    }
}

